var datas = [];

var isUpate = false; // 是否是修改，默认不是
var currentRow;

var search_data = {
    page: 1, limit: 10,
    name: stitle.value,
    code: stitle.value
};

loadData(search_data);

function showData(arr) {
    list_div.innerHTML = "";
    if (arr.length == 0) {
        list_div.innerHTML = "暂无数据";
        return;
    }
    var allhtml = "";
    allhtml += "<table cellpadding='0' cellspacing='0'" +
        " width='100%'><tr class='theader'>" +
        "<th>战区名称</th><th>战区编码</th><th>负责人</th>" +
        "<th>负责人联系方式</th><th>操作</th></tr>";
    for (var i = arr.length - 1; i >= 0; i--) {
        var a = arr[i];
        allhtml +=
            "<tr class='list'>" +
            "<td class='ntitle'>" + a.name + "</td>" +
            "<td class='ntitle'>" + a.code + "</td>" +
            "<td class='ntitle'>" + a.admin_name + "</td>" +
            "<td class='ntitle'>" + a.admin_tel + "</td>" +
            "<td class='ntitle'><a class='edit_a' onclick='upd(" + i + ")' href='javascript:;'>编辑</a>" +
            "<a class='del_a' onclick='del(" + a.id + ")' href='javascript:;'>删除</a></td>" +
            "</tr>";
    }
    allhtml += "</table>";
    list_div.innerHTML = allhtml;
}

function search() {
    search_data = {
        page: 1, limit: 10, name: stitle.value,
        code: stitle.value
    }
    loadData(search_data);
}

function loadData(data) {
    list_div.innerHTML = "";
    loading.style.display = "block";
    var req = new XMLHttpRequest();
    req.open("post", "/find_all_zhanqu");
    req.setRequestHeader("content-Type", "application/json;charset=utf-8");
    if (!data) data = {}
    req.send(JSON.stringify(data));
    req.onreadystatechange = function () {
        if (req.readyState == 4 && req.status == 200) {
            datas = JSON.parse(req.responseText);
            showData(datas);
            loading.style.display = "none";
        }
    }

    ajax("count_all_zhanqu", search_data, function (data) {
        // console.log(data);  33
        data = parseInt(data);
        current_page.innerHTML = search_data.page;
        total_page.innerHTML =
            (data % search_data.limit == 0 ?
                data / search_data.limit :
                Math.floor(data / search_data.limit) + 1);
    });
}

function next_page() {
    if (search_data.page < parseInt(total_page.innerHTML)) {
        search_data.page++;
        loadData(search_data);
    }
}

function pre_page() {
    if (search_data.page >= 2 && search_data.page <= parseInt(total_page.innerHTML)) {
        search_data.page--;
        loadData(search_data);
    }
}

function index_page() {
    search_data.page = 1;
    loadData(search_data);
}

function tail_page() {
    search_data.page = parseInt(total_page.innerHTML);
    loadData(search_data);
}

function to_page() {
    var ip = parseInt(input_page.value);
    if (ip >= 1 && ip <= parseInt(total_page.innerHTML)) {
        search_data.page = ip;
        loadData(search_data);
    }
}

function save() {
    ajax(isUpate ? "upd_zhanqu" : "add_zhanqu", {
        id: currentRow ? currentRow.id : -1,
        name: zname.value,
        code: code.value,
        admin_name: admin_name.value,
        admin_tel: admin_tel.value
    }, function (data) {
        alert(isUpate ? "更新成功！" : "新增成功！");
        loadData(search_data);
        add_div.style.display = "none";
        list_div.style.display = "block";
        save_btn.style.display = "none";
        add_btn.style.display = "block";
        search_div.style.display = "block";
        return_btn.style.display = "none";
    })

}


function del(id) {
    if (!confirm("您确定要删除这个数据吗？")) {
        return;
    }
    ajax("/del_zhanqu", {id: id}, function (data) {
        alert("删除成功！");
        loadData(search_data);
    });
}

function add() {
    isUpate = false;
    add_div.style.display = "block";
    list_div.style.display = "none";
    save_btn.style.display = "block";
    add_btn.style.display = "none";
    search_div.style.display = "none";
    return_btn.style.display = "block";

    zname.value = "";
    code.value = "";
    admin_name.value = "";
    admin_tel.value = "";
    zname.focus();
}

function returnMain() {
    list_div.style.display = "block";
    add_div.style.display = "none";
    search_div.style.display = "block";
    return_btn.style.display = "none";
    save_btn.style.display = "none";
    add_btn.style.display = "block";
}

function upd(i) {
    isUpate = true;
    var d = datas[i];
    currentRow = d;
    add_div.style.display = "block";
    list_div.style.display = "none";
    save_btn.style.display = "block";
    add_btn.style.display = "none";
    search_div.style.display = "none";
    return_btn.style.display = "block";

    zname.value = d.name;
    code.value = d.code;
    admin_name.value = d.admin_name;
    admin_tel.value = d.admin_tel;
    zname.focus();
}